#include "BossStage1.h"
#include "../Player.h"

#define SHOT_DELAY 1.5f
#define MOVEMENT_DELAY 2.5f

CBossStage1::CBossStage1(void)
{

}

CBossStage1::~CBossStage1(void)
{

}

//Inherited from CEntity///////////////////////////////
void CBossStage1::Update (float fElapsedTime)
{
	if(this->m_nHealth <= 0)
	{
		//Call SummonSystem

		CDestroyEntityMsg* pMsg = new CDestroyEntityMsg(this);
		CMessageSystem::GetInstance()->SendMsg(pMsg);
		pMsg = nullptr;
	}


	
	m_fTimer += fElapsedTime;
	if(m_fTimer>=SHOT_DELAY)
	{
		m_fShootTimer=0;

	}

	CEntity::Update(fElapsedTime);
	m_pAM->Update(fElapsedTime,this->m_cAniInfo);
	//Get current animations
	this->SetHeight(m_pAM->GetCHeight(m_cAniInfo));
	this->SetWidth(m_pAM->GetCWidth(m_cAniInfo));


	// CHANGE UP BASED ON CLone level.
	if(this->GetPosY() + this->GetHeight() < CGame::GetInstance()->GetHeight() && this->OnPlatform() == false)
		this->SetVelY(this->GetVelY()+1);
	if(this->GetPosY() + this->GetHeight() > (int)CGame::GetInstance()->GetHeight())
		this->SetPosY((float)CGame::GetInstance()->GetHeight()-this->GetHeight());
	if(this->GetPosX() <= 0)
		if(this->GetVelX() < 0)
		{
			this->SetVelX(0);
			this->SetPosX(0);
		}
		if(this->GetPosX() + this->GetWidth() >= CGame::GetInstance()->GetWidth())
			if(this->GetVelX() > 0)
			{
				this->SetVelX(0);
				this->SetPosX((float)CGame::GetInstance()->GetWidth()-this->GetWidth());
			}
			if(this->m_pTarget->GetPosX() + m_pTarget->GetWidth() < this->GetPosX())
				this->SetVelX(300);
			else if(this->m_pTarget->GetPosX() > this->GetPosX() + this->GetWidth())
				this->SetVelX(-300);
}

void CBossStage1::Render (void)
{
	if(this->m_nCloneLevel==0)
	m_pAM->Render(m_cAniInfo,(int)this->GetPosX(),(int)this->GetPosY(),1,this->GetFacing());
	else if (this->m_nCloneLevel==1)
	m_pAM->Render(m_cAniInfo,(int)this->GetPosX(),(int)this->GetPosY(),0.75,this->GetFacing());
	else if (this->m_nCloneLevel==2)
	m_pAM->Render(m_cAniInfo,(int)this->GetPosX(),(int)this->GetPosY(),0.5,this->GetFacing());
}

RECT CBossStage1::GetRect(void) const
{

	//RECT temp = { 0,0,0,0 };
	//return temp;
	RECT collision = {(int)this->GetPosX()- (int)(GetWidth()*0.5f), (int)this->GetPosY()-int(GetHeight()*0.5f), (int)this->GetPosX()+ int(GetWidth()*0.5), (int)this->GetPosY()+int(GetHeight()*0.5f)  };
	return collision;
}

bool CBossStage1::CheckCollision( IEntity* pOther )
{
	return true;
}
///////////////////////////////////////////////////////
void CBossStage1::HandleEvent(CNEvent* pEvent)
{
	if(pEvent->GetEventID() == "Summon")
	{
		if(this->m_nCloneLevel==0)
			m_pSS->Spawn(this->m_pTarget,"Base",4,(int)this->GetPosX()+10,(int)this->GetPosY(),2);
		if(this->m_nCloneLevel==1)
			m_pSS->Spawn(this->m_pTarget,"Boss1",6,(int)this->GetPosX()+10,(int)this->GetPosY(),2);
		if(this->m_nCloneLevel==2)
			m_pSS->Spawn(this->m_pTarget,"Boss1",8,(int)this->GetPosX()+10,(int)this->GetPosY(),2);
	}
	if(pEvent->GetEventID() == "Clone")
	{
		if(this->m_nCloneLevel==0)
			m_pSS->Spawn(this->m_pTarget,"Boss1",2,(int)this->GetPosX(),(int)this->GetPosY(),1);
		if(this->m_nCloneLevel==1)
			m_pSS->Spawn(this->m_pTarget,"Boss1",2,(int)this->GetPosX(),(int)this->GetPosY(),2);
	}
}
bool CBossStage1::Init(CEntity* pTarget,int x,int y,int speedset)
{
	if(pTarget->GetType() != OBJ_PLAYER)
		return false;
	else
	{
		m_pES = CEventSystem::GetInstance();
		m_pTM	= CSGD_TextureManager::GetInstance();
		m_pXA	= CSGD_XAudio2::GetInstance();
		m_pD3D	= CSGD_Direct3D::GetInstance();
		m_pDI	= CSGD_DirectInput::GetInstance();
		m_pMS	= CMessageSystem::GetInstance();
		m_pAM	= CAnimationManager::GetInstance();
		m_pSS	= CSpawnSystem::GetInstance();

		CEventSystem::GetInstance()->RegisterClient("Summon", this); 
		CEventSystem::GetInstance()->RegisterClient("Clone", this);


		//Set planned animations
			m_cAniInfo.SetName("EBoss1Move");
			m_cAniInfo.SetNext("EBoss1Summon");
			m_cAniInfo.SetTime(0);
			m_cAniInfo.SetFrame(0);

			this->SetHeight(m_pAM->GetCHeight(m_cAniInfo));
			this->SetWidth(m_pAM->GetCWidth(m_cAniInfo));

		m_eType = OBJ_BOSS1;
		this->SetFacing(true);
		this->SetDensity(1);

		this->SetPosX(float(x));
		this->SetPosY(float(y));//(float)CGame::GetInstance()->GetHeight() - this->GetHeight()-500);

		this->SetVelX(0);
		this->SetVelY(0);
		this->m_pTarget = pTarget;
		this->OnPlatform(false);
		m_fTimer =0;
		if(speedset==0)
		{
			this->m_nHealth = 400;
			this->m_fShootTimer =0.75f;
			this->m_nCloneLevel=0;
		}
		else if(speedset==1)
		{
			this->m_nHealth = 200;
			this->m_fShootTimer =0.50f;
			this->m_nCloneLevel=1;
		}
		else if(speedset==2)
		{
			this->m_nHealth = 100;
			this->m_fShootTimer =0.25f;
			this->m_nCloneLevel=2;
		}
	}
	return true;

}
